<template>
  <div>

    <div class="card" style="margin-bottom: 10px; background-color: bisque">
      <el-input style="width: 300px;margin-right: 10px" v-model="data.name" placeholder="请输入课程名称查询" :prefix-icon="Search"/>
      <el-input style="width: 300px;margin-right: 10px" v-model="data.no" placeholder="请输入课程编号查询" :prefix-icon="Search"/>
      <el-button type="primary" style="margin-left: 10px" @click="load">查询</el-button>
      <el-button type="info" @click="reset">重置</el-button>
    </div>

    <div class="card" style="margin-bottom: 10px; background-color: bisque">

      <el-table stripe :data="data.tableData" :cell-style="cellStyle" >
        <el-table-column label="序号" prop="id"></el-table-column>
        <el-table-column label="课程名称" prop="name"></el-table-column>
        <el-table-column label="课程编号" prop="no"></el-table-column>
        <el-table-column label="学生姓名" prop="studentName"></el-table-column>
        <el-table-column label="操作" align="center" width="180">
          <template #default="scope">
            <el-button type="primary" plain @click="del(scope.row.id)">删除</el-button>
            <el-button type="primary" plain @click="addGrade(scope.row)" v-if="data.user.role === 'ADMIN'">评分</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--    分页-->
    <div class="card" style="background-color: bisque">
      <el-pagination v-model:current-page="data.pageNum" v-model:page-size="data.pageSize"
                     @current-change="handelCurrentChange"
                     background layout="prev, pager, next" :total="data.total" />
    </div>
    <el-dialog with="35%" v-model="data.formVisible" title="成绩信息" width="40%">
      <el-form :model="data.gradeForm" label-with="100px" style="padding-right: 30px">
        <el-form-item label="课程名称" :label-width="formLabelWidth">
          <el-input v-model="data.gradeForm.name" autocomplete="off" disabled/>
        </el-form-item>
        <el-form-item label="分数" :label-width="formLabelWidth">
          <el-input v-model="data.gradeForm.score" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="评语" :label-width="formLabelWidth">
          <el-input type="textarea" v-model="data.gradeForm.comment" autocomplete="off"/>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="data.formVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">保 存</el-button>
      </span>
      </template>
    </el-dialog>

  </div>
</template>

<script setup>
import request from "@/utils/request";
import {reactive} from "vue";
import {Search} from "@element-plus/icons-vue";
import {ElMessage, ElMessageBox} from "element-plus";

// request.get('/').then(res => {
//   console.log(res)
// })

const data = reactive({
  name:'',
  no:'',
  pageNum: 1,
  pageSize:5,
  tableData: [],
  total: 0,
  user:JSON.parse(localStorage.getItem('student-user')||'{}'),
  gradeForm: {},
  formVisible: false
})
const load = () =>{
  let params = {
    pageNum: data.pageNum,
    pageSize: data.pageSize,
    name:data.name,
    no:data.no,
  }
  if(data.user.role === 'STUDENT') {
    params.studentId = data.user.id
  }
  request.get('/studentCourse/selectPage',
      {
        params: params
      }).then(res=>{
    //console.log(res)
    data.tableData=res.data?.list || []
    data.total=res.data?.total || 0
  })
}
//调用后台数据
load()
const handelCurrentChange = () => {
  // 当翻页的时候重新加载数据即可
  load()
}
const reset =() => {
  data.name = ''
  data.no = ''
  load()
}
const del =(id) => {
  ElMessageBox.confirm('删除数据后无法恢复，您确认删除吗？', '删除确认', { type: 'warning' }).then(res => {
    request.delete('/studentCourse/delete/' + id).then(res => {
      if (res.code === '200') {
        load()    // 重新获取数据
        ElMessage.success("操作成功")
      } else {
        ElMessage.error(res.msg)
      }
    })
  }).catch(res => {
    ElMessage({
      type: 'info',
      message: '您已取消',
    })
  })
}

const  addGrade =(row) => {
  data.gradeForm = {}
  data.formVisible = true
  data.gradeForm.name = row.name
  data.gradeForm.courseId = row.courseId
  data.gradeForm.studentId = row.studentId
}
const save = () => {
  request.post('/grade/add', data.gradeForm).then(res => {
    if (res.code === '200') {
      data.formVisible = false
      ElMessage.success("操作成功")
    } else {
      ElMessage.error(res.msg)
    }
  })
}
const cellStyle = () => {
  return { backgroundColor: "bisque", color: "#333333" };
};

</script>